package org.icop.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.jbpt.graph.abs.IEdge;
import org.jbpt.graph.abs.IGraph;
import org.jbpt.hypergraph.abs.IVertex;

public class AnnotationHandler {

	public static final String ANNOTATION_ID = "id";
	public static final String ANNOTATION_LABEL = "label";
	public static final String ANNOTATION_DATA = "data";
	public static final String ANNOTATION_INPUT = "input";
	public static final String ANNOTATION_OUTPUT = "output";
	public static final String ANNOTATION_DESCRIPTION = "description";
	public static final String ANNOTATION_RESPONSIBLE = "responsible";
	
	public static Set<String> ANNOTATIONS = new HashSet<>();
	
	static {
		ANNOTATIONS.add(ANNOTATION_ID);
		ANNOTATIONS.add(ANNOTATION_LABEL);
		ANNOTATIONS.add(ANNOTATION_DATA);
		ANNOTATIONS.add(ANNOTATION_INPUT);
		ANNOTATIONS.add(ANNOTATION_OUTPUT);
		ANNOTATIONS.add(ANNOTATION_DESCRIPTION);
		ANNOTATIONS.add(ANNOTATION_RESPONSIBLE);
	}
	
	public static Map<String, String> parseAnnotationMapFromString(String text) {
		Map<String, String> result = new HashMap<String, String>();
		
		String[] entries = text.split(";");
		for (int i = 0; i < entries.length; i++ ) {
			String entry = entries[i];
			int del = entry.indexOf("=");
			if (del != -1) {
				String id = entry.substring(0,del);
				String body = entry.substring(del+1,entry.length());
				
				if (ANNOTATIONS.contains(id))
					result.put(id, body);
			}
		}
		
		return result;
	}
	
	public static 
		<G extends IGraph<E,V>, E extends IEdge<V>, V extends IVertex> 
		Map<V, Map<String, String>> getAnnotationsForGraph(G g) {
		
		Map<V, Map<String, String>> result = new HashMap<>();
		
		for (V v: g.getVertices())
			result.put(v, parseAnnotationMapFromString(v.getDescription()));
		
		return result;
	}
	

}
